package com.itbaizhan.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * 对话控制层
 */
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    private ChatModel chatClient;


    /**
     * 流式输出
     * @param message
     * @return
     */
    @GetMapping("/chat")
    public String chat(String input) {
        ChatResponse response = chatClient.call(new Prompt(input));
        return response.getResult().getOutput().getContent();
    }

    @GetMapping("/stream")
    public Flux<ChatResponse> stream(String input) {
        Flux<ChatResponse> stream = chatClient
                .stream(new Prompt(input));
        return stream;
    }
}
